package com.dsx.haha.controller;

import com.dsx.haha.model.entity.TenantUser;
import com.dsx.haha.model.entity.User;
import com.dsx.haha.service.TenantService;
import com.dsx.haha.service.TenantUserService;
import com.dsx.haha.service.UserService;
import com.dsx.haha.utils.RedPrint;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @author dsx
 * @date 2021/3/14 11:55
 * @since JDK1.8
 */
@Controller
@RequestMapping("/function")
public class TenantController {
    @Reference
    UserService userService;
    @Reference
    TenantService tenantService;
    @Reference
    TenantUserService tenantUserService;
    
    //跳转到添加邀请用户界面
    @RequestMapping("/toinvitation")
    public String toInvitation(String tid, Model model){
        model.addAttribute("tid",tid);
        return "/function/invitation";
    }
    
    //邀请用户加入租户
        //用户不存在
        //用户已经在这个租户中了
        //邀请加入成功
    @PostMapping("/invitation")
    @ResponseBody//返回值json
    public String invitation(String tenantid, String username, HttpSession session){
        if (username.equals(session.getAttribute("username"))){
            return "不能邀请自己！";
        }
        User user = userService.getUser(username);
        if(user==null){
            return "邀请的用户不存在！！！";
        }
        int i = tenantUserService.checkUser(user.getId(), tenantid);
        if(i==-1){
            return "邀请成功！！！";
        }
        return "被邀请用户已经存在，不能重复邀请";
        
    }
}
